/*
Java source file
Copyright: Umschalt.com 2012
*/
package com.david.documentation.web.view.panels;

import com.david.documentation.domain.entities.category.Category;
import com.david.documentation.domain.entities.doku.Doku;
import com.david.documentation.domain.entities.dokulinks.DokuLinks;
import com.david.documentation.domain.entities.episode.Episode;
import com.david.documentation.domain.entities.tags.Tag;
import com.david.documentation.service.category.CategoryService;
import com.david.documentation.service.tag.TagService;
import com.david.documentation.web.view.DokuUtils;
import com.david.documentation.web.view.pages.CategoryPage;
import com.david.documentation.web.view.pages.TagPage;
import org.apache.commons.lang.StringUtils;
import org.apache.wicket.Component;
import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.extensions.ajax.markup.html.AjaxLazyLoadPanel;
import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.link.PopupSettings;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.spring.injection.annot.SpringBean;

public class DokuPanel extends Panel{

	private static final long serialVersionUID = 7357598082541197703L;

	@SpringBean
	private CategoryService categoryService;

	@SpringBean
	private TagService tagService;

	private String search;

	public DokuPanel(String id, Doku doku,String search) {
		super(id);
		this.search = search;

		if (search != null && StringUtils.isNotEmpty(search)){
			add(new Label("title", DokuUtils.markSearchString(search, doku.getName())).setEscapeModelStrings(false));
			add(new Label("description",DokuUtils.markSearchString(search, doku.getDescription())).setEscapeModelStrings(false));
		} else{
			add(new Label("title", doku.getName()));
			add(new Label("description",doku.getDescription()));
		}
				
		add(new WebMarkupContainer("image").add(new SimpleAttributeModifier("src",doku.getImage_url())));
		add(new Label("duration",doku.getDuration()));
		add(new Label("format",doku.getFormat()));
		add(new Label("size",doku.getSize()));
		add(new Label("language",doku.getLanguage()));
		add(new Label("uploader",doku.getUploader()));

		ListView<Category> categorys = new ListView<Category>("categorys",doku.getCategorys()) {

			@Override
			protected void populateItem(ListItem<Category> dokuListItem) {
				final Category cat = dokuListItem.getModelObject();
				AjaxLazyLoadPanel countPanel = new AjaxLazyLoadPanel("catCount") {
					@Override
					public Component getLazyLoadComponent(String markupId) {
						return new Label(markupId,"("+categoryService.getNumberOfDokus(cat.getId())+")");
					}
				};
				dokuListItem.add(new BookmarkablePageLink("category", CategoryPage.class,new PageParameters("category="+cat.getName()))
						.add(new Label("catLabel",cat.getName())).add(countPanel));
			}
		};
		add(categorys);

		ListView<Tag> tags = new ListView<Tag>("tags",doku.getTags()) {
			@Override
			protected void populateItem(ListItem<Tag> tagListItem) {
				final Tag tag = tagListItem.getModelObject();

				AjaxLazyLoadPanel countTagPanel = new AjaxLazyLoadPanel("tagCount") {
					@Override
					public Component getLazyLoadComponent(String markupId) {
						return new Label(markupId,"("+tagService.getNumberOfDokus(tag.getId())+")");
					}
				};

				tagListItem.add(new BookmarkablePageLink("tag",TagPage.class,new PageParameters("tag="+tag.getName()))
					.add(new Label("tagLabel",tag.getName())).add(countTagPanel));
			}
		};
		add(tags);

		RepeatingView episodes = new RepeatingView("episodes");
		for (Episode episode : doku.getEpisodes()){
			RepeatingView links = new RepeatingView("links");
			WebMarkupContainer episodeContainer = new WebMarkupContainer(episodes.newChildId());
			episodeContainer.add(new Label("episodeName",episode.getName()));
			for (final DokuLinks link : episode.getDokuLinks()){

				WebMarkupContainer cont = new WebMarkupContainer(links.newChildId());

				PopupSettings settings = new PopupSettings(PopupSettings.RESIZABLE | PopupSettings.SCROLLBARS);
				settings.setHeight(400).setWidth(800).setWindowName("DOWNLOAD");

				cont.add(new ExternalLink("link", link.getLink(), "Download (" + link.getHoster()+")").setPopupSettings(settings));

				final Panel[] captchaPanel = {new AjaxLazyLoadPanel("captcha"){

					@Override
					public Component getLazyLoadComponent(String markupId) {
						return new CaptchaPanel(markupId,link.getLink());
					}
				}};
				captchaPanel[0].setOutputMarkupId(true);
				cont.add(captchaPanel[0]);

				cont.add(new IndicatingAjaxLink("refresh") {

					@Override
					public void onClick(AjaxRequestTarget target) {
						CaptchaPanel tmp = new CaptchaPanel("captcha",link.getLink());
						tmp.setOutputMarkupId(true);
						captchaPanel[0].replaceWith(tmp);
						captchaPanel[0] = tmp;
						captchaPanel[0].setOutputMarkupId(true);
						target.addComponent(captchaPanel[0]);

						info("Fehler / Captcha falsch - versuchs doch nochmal");
						target.addChildren(getPage(), FeedbackPanel.class);
					}
				});

				links.add(cont);
				episodeContainer.add(links);
			}
			episodes.add(episodeContainer);
		}
		add(episodes);

		add(new AjaxLink("setjdownloaderState") {

			@Override
			public void onClick(AjaxRequestTarget target) {
				info("Starte Jdownloader um den Download mit einem Klick zu starten");
				target.addChildren(getPage(), FeedbackPanel.class);
			}
		}.setMarkupId("setjdownloaderState"));

	}




}
